package com.hatcher.cvte.controller;

import com.hatcher.cvte.model.BaseResult;
import com.hatcher.cvte.model.Dept;
import com.hatcher.cvte.model.Menu;
import com.hatcher.cvte.model.User;
import com.hatcher.cvte.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

@Controller
public class UserController {

	@Autowired
	private UserService userService;

	/**
	 * 用户登录接口
	 * @param user
	 * @param captcha
	 * @param session
	 * @return
	 */
	@RequestMapping(value = "isLogin")
	@ResponseBody
	public BaseResult isLogin(User user, String captcha, HttpSession session) {
		String yzm = (String) session.getAttribute(com.google.code.kaptcha.Constants.KAPTCHA_SESSION_KEY);
		/** 默认是false **/
		System.out.println(user.getLoginAccount());
		BaseResult result = new BaseResult();
		if (yzm.equals(captcha)) {
			User value = userService.islogin(user);
			if (value.getUserCode() != null) {
				result.setResultMsg("success");
				result.setResult(true);
				session.setAttribute("USERCODE", value.getUserCode());
			}
			System.out.println("后台验证中。。。");
		} else {
			result.setResultMsg("验证码错误，请重新输入！");
		}
		return result;
	}

	/**
	 * 获取用户部门数据，乱序
	 *
	 * @param session
	 * @return
	 */
	@RequestMapping("getUserDepts")
	@ResponseBody
    public List<Dept> getUserDepts(HttpSession session) {
        String userCode = (String) session.getAttribute("USERCODE");
        List<Dept> list = userService.getUserDepts(userCode);
        System.out.println("获取部门信息中。。。");
        return list;
    }

	/**
	 * 获取用户部门数据json字符串(拥有父子结构的树结构)
	 *
	 * @param session
	 * @param response
	 */
	@RequestMapping("getDeptTree")
	public void getDeptTreeRoot(HttpSession session, HttpServletResponse response) {
		String userCode = (String) session.getAttribute("USERCODE");
		String result = userService.getDeptTreeRoot(userCode);
		System.out.println(result);
		try {
			response.setCharacterEncoding("UTF-8");
			PrintWriter writer = response.getWriter();
			writer.write(result);
		} catch (IOException e) {
			e.printStackTrace();
		}
		System.out.println("获取部门信息中。。。");
	}

	/**
	 * 获取用户菜单数据
	 *
	 * @param session
	 * @return
	 */
	@RequestMapping("getUserMenus")
	@ResponseBody
	public List<Menu> getUserMenus(HttpSession session) {
		String userCode = (String) session.getAttribute("USERCODE");
		List<Menu> list = userService.getUserMenus(userCode);
		System.out.println("获取菜单信息中。。。");
		return list;
	}

	/**
	 * 退出登录
	 * @param session
	 * @return
	 */
	@RequestMapping("exit")
	public ModelAndView exit(HttpSession session) {
		ModelAndView view = new ModelAndView("index");
		session.removeAttribute("USERCODE");
		return view;
	}
}
